﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace BootyBay.PrivateStoreApp.Controls
{
    public class ImageButton : Button
    {
        private Image m_Image;

        #region Properties

        /// <summary>
        /// 悬浮态
        /// </summary>
        public string ImageOver
        {
            get { return (string)GetValue(ImageOverProperty); }
            set { SetValue(ImageOverProperty, value); }
        }

        /// <summary>
        /// 正常态
        /// </summary>
        public string ImageNormal
        {
            get { return (string)GetValue(ImageNormalProperty); }
            set { SetValue(ImageNormalProperty, value); }
        }

        /// <summary>
        /// 点击态
        /// </summary>
        public string ImageDown
        {
            get { return (string)GetValue(ImageDownProperty); }
            set { SetValue(ImageDownProperty, value); }
        }

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty ImageNormalProperty =
            DependencyProperty.Register("ImageNormal", typeof(string), typeof(ImageButton), new PropertyMetadata(null));

        public static readonly DependencyProperty ImageOverProperty =
            DependencyProperty.Register("ImageOver", typeof(string), typeof(ImageButton), new PropertyMetadata(null));

        public static readonly DependencyProperty ImageDownProperty =
            DependencyProperty.Register("ImageDown", typeof(string), typeof(ImageButton), new PropertyMetadata(null));


        public static readonly DependencyProperty IsSetEnableProperty =
            DependencyProperty.Register("IsSetEnable", typeof(bool?), typeof(ImageButton), new PropertyMetadata(null, IsSetEnablePropertyChanged));

        private static void IsSetEnablePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageButton imageButton = d as ImageButton;
            if (imageButton == null)
            {
                return;
            }
            if ((bool?)e.NewValue == true)
            {
                imageButton.ChangeImageSource(imageButton.ImageNormal);
            }
            else
            {
                imageButton.ChangeImageSource(imageButton.ImageDown);
            }
        }

        public bool? IsSetEnable
        {
            get { return (bool?)GetValue(IsSetEnableProperty); }
            set { SetValue(IsSetEnableProperty, value); }
        }
        #endregion

        public ImageButton()
        {
            DefaultStyleKey = typeof(ImageButton);
            this.PointerEntered += ImageButton_PointerEntered;
            this.PointerExited += ImageButton_PointerExited;
            this.PointerPressed += ImageButton_PointerPressed;
            this.PointerReleased += ImageButton_PointerReleased;
            this.PointerCaptureLost += ImageButton_PointerCaptureLost;
            IsEnabledChanged += ImageButton_IsEnabledChanged;
        }

        void ImageButton_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            ChangeImageSource(ImageDown);
        }
        void ImageButton_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            ChangeImageSource(ImageOver);
        }

        void ImageButton_PointerEntered(object sender, PointerRoutedEventArgs e)
        {
            ChangeImageSource(ImageOver);
        }

        void ImageButton_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            Control control = sender as Control;
            if (control.IsEnabled)
            {
                ChangeImageSource(ImageNormal);
            }
        }

        void ImageButton_PointerCaptureLost(object sender, PointerRoutedEventArgs e)
        {
            ChangeImageSource(ImageNormal);
        }

        void ImageButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                ChangeImageSource(ImageNormal);
            }
            else
            {
                ChangeImageSource(ImageDown);
            }
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            m_Image = GetTemplateChild("PART_img") as Image;
            ChangeImageSource(ImageNormal);
        }

        public void ChangeImageSource(string imageUri)
        {
            if (m_Image != null)
            {
                if (imageUri != null)
                    m_Image.Source = new BitmapImage(new Uri(imageUri, UriKind.RelativeOrAbsolute));
            }
        }

        public void SetDisable()
        {
            this.IsEnabled = true;
            this.IsEnabled = false;
        }

        public void SetEnable()
        {
            this.IsEnabled = false;
            this.IsEnabled = true;
        }
    }
}
